<template>
    <div class="min-h-screen flex flex-col">
        <!-- 登录注册 -->
        <header class="header bg-white p-4 shadow-md flex justify-between items-center">

            <div class="container flex items-center">
                <div class="links-container">
                    <a href="#" class="flex items-center text-gray-600 hover:text-blue-500">
                        <i class="icon-download"></i>
                        <span class="ml-1">下载App</span>
                    </a>
                    <a href="#" class="text-blue-500 font-bold hover:underline">栏目收费说明</a>
                </div>
                <!-- 使用 v-if/v-else 来条件渲染登录/注册按钮或用户信息 -->
                <div v-if="!username" class="lrcs items-center space-x-4"> <!-- Flex 布局让内容一行排列 -->
                    <RouterLink class="btn-login"
                        :to="{ name: 'Login', query: { redirecturl: router.currentRoute.value.fullPath } }">登录
                    </RouterLink>
                    <RouterLink class="btn-register" to='/register'>注册</RouterLink>
                </div>
                <div v-else class="user-info flex items-center space-x-3"> <!-- 用户信息 -->
                    <img :src="avatar" alt="用户头像" class="user-avatar w-8 h-8 rounded-full" />
                    <span>{{ username }}</span>
                    <span class="text-logout" @click="logout">退出</span>
                </div>

            </div>
        </header>

        <!-- 搜索 -->
        <div class="search-header">
            <div class="search-wrapper">
                <RouterLink to="/" class="text-a" >丁香医生</RouterLink>
                <input type="text" v-model="searchQuery" placeholder="搜索疾病、医院、药品" class="search-input" />
                <button class="search-button" @click="search(searchQuery)">搜索</button>
            </div>
        </div>

        <!-- 顶部导航 -->
        <nav class="main-nav">
            <router-link v-for="item in navigationItems" :key="item.name" :to="item.link" class="nav-link">
                {{ item.name }}
            </router-link>
        </nav>

        <!-- 面包屑 -->
        <div class="breadcrumb-container mb-4">
            <nav class="flex items-center text-sm">
                <router-link to="/" class="text-emerald-500 hover:text-emerald-600">首页</router-link>
                <span class="mx-2 text-gray-400">›</span>
                <router-link to="/examination" class="text-emerald-500 hover:text-emerald-600">查检查/手术</router-link>

            </nav>

            <!-- 分类 -->
            <div class="qa-container">
                <div class="content-wrapper">
                    <div class="categories-container">
                        <div class="category-box">

                            <div class="category-content">
                                <button v-for="item in opcategory" :key="item.id" @click="selectOpcategory(item)"
                                    :class="['category-btn', selectedOpcategory?.id === item.id ? 'active' : '']">
                                    {{ item.name }}
                                </button>
                            </div>
                        </div>
                    </div>

                    <!-- 检查列表 -->
                    <div class="main-content-area">
                        <div class="operation-list-box"> 

                            <div class="operation-list-item" v-for="operation in operations" :key="operation.id"
                                @click="navigateToOperation(operation.id)">
                                <h4 class="operation-item-title">{{ operation.name }}</h4>
                                <p class="operation-item-content" v-html="operation.remark.slice(0, 40) + '...'"></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Footer -->
            <footer class="mt-auto">
                <div class="bottom-links">
                    <a target="_blank" href="http://www.dxy.cn/pages/copyright.html" rel="nofollow">版权声明</a>
                    <a target="_blank" href="https://dxy.com/column/board" rel="nofollow">作者编委</a>
                    <a target="_blank" href="https://ask.dxy.com/ama/index" rel="nofollow">丁香医生</a>
                    <a href="mailto:doctor@dxy.com" rel="nofollow">投稿邮箱 doctor@dxy.com</a>
                    <a target="_blank">客服电话：0571-22931767</a>
                </div>
            </footer>
        </div>


    </div>


</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { jwtDecode } from 'jwt-decode';
import { RouterLink } from 'vue-router';

const router = useRouter();
const username = ref<string | null>(null);
const avatar = ref<string | null>(null);
const searchQuery = ref('');

const navigationItems = [
    { name: '问医生', link: '/sections' },
    { name: '查疾病', link: '/disease' },
    { name: '查药品', link: '/medicine' },
    { name: '查医院', link: '/hospital' },
    { name: '查检查/手术', link: '/examination' },
    { name: '查疫苗', link: '/vaccine' },
    { name: '急救指南', link: '/emergency' },
    { name: '测一测', link: '/test' },
    { name: '科普视频', link: '/videos' }
];

interface Opcategory {
    id: number;
    name: string;
}

interface Operation {
    id: number;
    name: string;
    remark: string;
}

const opcategory = ref<Opcategory[]>([]);
const operations = ref<Operation[]>([]);
const selectedOpcategory = ref<Opcategory | null>(null);
const selectedOperation = ref<Operation | null>(null);


const fetchUserInfo = async () => {
    const token = localStorage.getItem('token');
    if (token) {
        try {
            const decodedToken = await jwtDecode(token);

            const redirecturl = router.currentRoute.value.path
            console.log(redirecturl)
            router.push({name: 'Examination', query: {redirecturl: redirecturl}});

            // 假设 jwtDecode 是异步的
            username.value = decodedToken.username;
            avatar.value = 'http://127.0.0.1:8000/uploads/' + decodedToken.avatar;
            console.log(avatar.value);
        } catch (error) {
            console.error('Error decoding JWT:', error);
        }
    }
}

const logout = () => {
    localStorage.removeItem('token');
    username.value = null;
    avatar.value = null;
};

const fetchOpcategory = async () => {
    const res = await axios.get('http://127.0.0.1:8000/doctor/opcategory/');
    opcategory.value = res.data;
    if (opcategory.value[0]) {
        selectOpcategory(opcategory.value[0]);
    }

}

const selectOpcategory = (opcategory: Opcategory) => {
    selectedOpcategory.value = opcategory;
    if (selectedOpcategory.value) {
        fetchOperation(opcategory.id);
    }
};

const fetchOperation = async (opcategoryId:number) => {
    try {
        const res = await axios.get(`http://127.0.0.1:8000/doctor/operation/${opcategoryId}/`)
        operations.value = res.data;
    } catch (error) {
        console.error('Error fetching operations:', error);
    }

}

const navigateToOperation = (operationId: number) => {
    window.open(`/examination/${operationId}`);
};

// 搜索
const search = () => {
    console.log(searchQuery.value);
 }


onMounted(() => {
    fetchUserInfo();
    fetchOpcategory();
    fetchOperation(1);
});

</script>

<style scoped>
.header {
    background-color: #f7f7f7;
    color: rgba(0, 0, 0, 0.5);
    box-shadow: 0 3px 3px rgba(0, 0, 0, 0.1);
    padding: 16px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
}

.header a {
    /* a标签默认有下划线，删除下划线 */
    text-decoration: none;
    /* 字体颜色 */
    color: #666;
    /* 字体大小 */
    font-size: 14px;
    display: flex;
    align-items: center;
    /* 用于定义元素从一种样式过渡到另一种样式时的动画效果 */
    transition: color 0.3s ease;
}

.header a:hover {
    color: #00b38a;
    /* Match green color on hover */
}

.header a.text-blue-500 {
    color: #00b38a;
    /* Green color for special links */
    font-weight: bold;
    margin-left: 16px;
}

.text-logout {
    margin-left: 16px;
    background-color: transparent;
    /* 透明背景 */
    border: none;
    /* 无边框 */
    color: #666666;
    /* 文本颜色与下载链接一致 */
    padding: 0;
    /* 移除内边距 */
    font-size: 14px;
    font-weight: normal;
    /* 正常字体粗细 */
    cursor: pointer;
    text-decoration: none;
    /* 移除下划线 */
    transition: color 0.3s ease;
    /* 颜色过渡效果 */
}

.text-logout:hover {
    color: #00b38a;
    text-decoration: underline;

}

.container {
    display: flex;
    align-items: center;

    flex-wrap: nowrap;

    gap: 16px;
    margin-right: 350px;

}

/* 可以添加一个包含栏目、下载等链接的容器样式 */
.links-container {
    display: flex;
    align-items: center;
    /* 之间的间隔 */
    border: none;
}

.btn-login {
    background-color: transparent;
    /* 透明背景 */
    border: none;
    /* 无边框 */
    color: #666666;
    /* 文本颜色与下载链接一致 */
    padding: 0;
    /* 移除内边距 */
    font-size: 14px;
    font-weight: normal;
    /* 正常字体粗细 */
    cursor: pointer;
    text-decoration: none;
    /* 移除下划线 */
    transition: color 0.3s ease;
    /* 颜色过渡效果 */
}

.btn-register {
    background-color: transparent;
    /* 透明背景 */
    border: none;
    /* 无边框 */
    color: #666666;
    /* 文本颜色与下载链接一致 */
    padding: 0;
    /* 移除内边距 */
    font-size: 14px;
    font-weight: normal;
    /* 正常字体粗细 */
    cursor: pointer;
    text-decoration: none;
    /* 移除下划线 */
    transition: color 0.3s ease;
    /* 颜色过渡效果 */
    margin-left: 13px;
}

.lrcs {
    display: flex;
    align-items: center;

}

/* 添加伪类来模仿下载链接的hover效果 */
.btn-login:hover,
.btn-register:hover {
    color: #00b38a;
    /* 悬停时变为绿色 */
    text-decoration: underline;
    /* 悬停时添加下划线 */
}

/* User Info Styling */
.user-info {
    display: flex;
    align-items: center;
}

.user-info .user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    /* Circular avatar */
    object-fit: cover;

}

.user-info span {
    font-size: 14px;
    font-weight: 500;
    color: #333333;
}

/* 搜索 */
.search-header {
    background-color: #fff;
    padding: 20px 0;
}

.search-wrapper {
    max-width: 600px;
    margin: 0 auto;
    display: flex;
    gap: 10px;
}

.text-a {
    font-size: 32px;
    font-weight: 700;
    color: #00b38a;
    margin: 0 auto;
    letter-spacing: 1px;
    text-decoration: none;
}

.search-input {
    flex: 1;
    padding: 12px 20px;
    border: 2px solid #00b38a;
    border-radius: 24px;
    font-size: 16px;
    outline: none;
}

.search-button {
    padding: 12px 30px;
    background-color: #00b38a;
    color: white;
    border: none;
    border-radius: 24px;
    cursor: pointer;
    font-size: 16px;
}

/* 面包屑导航 */
.breadcrumb-container {
    max-width: 1000px;
    margin: 0 auto;
}

.breadcrumb-container nav .text-emerald-500 {
    transition: color 0.3s ease;
}

.breadcrumb-container nav .text-emerald-500:hover {
    color: #00b38a;
}

/* 顶部导航 */
.main-nav {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0;
    background-color: #fff;
    border-bottom: 1px solid #eee;
}

.nav-link {
    padding: 6px 16px;
    color: #666;
    text-decoration: none;
    font-size: 14px;
    transition: color 0.2s;
}

.nav-link:hover {
    color: #00b38a;
}

/* 底部 */
.bottom-links {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    padding: 24px;
    background-color: #f5f5f5;
}

.bottom-links a {
    color: #666;
    text-decoration: none;
    padding: 4px 8px;
}

.bottom-links a:hover {
    color: #00b38a;
}

/* 分类 */
.qa-container {
    background-color: #f8f9fa;
    min-height: 100vh;
    padding: 24px 0;
}

.content-wrapper {
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 20px;
}

.categories-container {
    margin-bottom: 32px;
}

.category-box {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    margin-bottom: 16px;
    padding: 20px;
}

.category-content {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

.category-btn {
    padding: 6px 16px;
    background-color: #f5f5f5;
    border: none;
    border-radius: 4px;
    color: #666;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.category-btn:hover {
    background-color: #e6f7f4;
    color: #00b38a;
}

.category-btn.active {
    background-color: #00b38a;
    color: white;
}

.main-content-area {
    display: flex;
    gap: 24px;
    margin-top: 24px;
}

.operation-list-item {
    background: white;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    transition: box-shadow 0.3s ease;
    

}

.operation-list-item:hover {
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);

}

.operation-item-title {
    font-size: 16px;
    font-weight: 500;
    color: #333;
}

.operation-item-content {
    font-size: 14px;
    color: #666;
    margin-top: 4px;
}

.operation-list-box {
    flex: 1;
    /* flex:1;就等于
        flex-grow: 1;
        flex-shrink: 1;
        flex-basis: 0%; */
}
</style>